package cn.b.sky.admin.service.impl;

import cn.b.sky.admin.dao.AdminECodeDao;
import cn.b.sky.admin.dao.AdminECodeTlDao;
import cn.b.sky.admin.dao.AdminECodeTypeDao;
import cn.b.sky.admin.model.*;
import cn.b.sky.admin.model.condition.ECodeTypeCondition;
import cn.b.sky.admin.service.AdminECodeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName: ECodeServiceImpl
 * @Description:枚举码操作实现类
 * @author: ZhaoXin
 * @date: 2015-7-27 上午10:34:34
 * @version: V1.0
 * 
 */
@Service("admineCodeService")
public class AdminECodeServiceImpl implements AdminECodeService {

  private static final Logger LOGGER = LoggerFactory.getLogger(AdminECodeServiceImpl.class);

  @Autowired
  private AdminECodeDao adminECodeDao;

  @Autowired
  private AdminECodeTypeDao adminECodeTypeDao;

  @Autowired
  private AdminECodeTlDao adminECodeTlDao;

  @Override
  public List<ECode> selectECodeList(String eCodeType, String languageCode) {
    if (!StringUtils.isEmpty(eCodeType)) {
      return adminECodeDao.selectECodeList(eCodeType, languageCode);
    } else {
      return null;
    }
  }

  @Override
  public ECode selectEcodeByECodeTypeAndLanguageCodeAndECode(String ecode, String eCodeType, String languageCode) {
    if (StringUtils.isEmpty(ecode)) {
      return null;
    }
    return adminECodeDao.selectEcodeByECodeTypeAndLanguageCodeAndECode(ecode, eCodeType, languageCode);
  }

  @Override
  public Map<String, ECode> selectECodeMap(String eCodeType, String languageCode) {
    
    List<ECode> ecodeList = this.selectECodeList(eCodeType, languageCode);
    Map<String, ECode> ecodeMap = new HashMap<String, ECode>(ecodeList.size());
    for (ECode ecode : ecodeList) {
      ecodeMap.put(ecode.geteCode(), ecode);
    }
    return ecodeMap;
  }

  @Override
  public String insertOrUpdateECodeTypeAndECode(ECodeType eCodeType) {
    
    ECodeType dbData = this.adminECodeTypeDao.getECodeTypeByPK(eCodeType.geteCodeType());
    if (null != dbData) {
      // 如果已经存在枚举码类型数据,则进行以下操作
      // 更新枚举码类型信息
      this.adminECodeTypeDao.updateECodeType(eCodeType);
    } else {
      // 新增枚举码及其关联信息
      // 新增枚举码类型信息
      this.adminECodeTypeDao.insertECodeType(eCodeType);
    }
    
    if (null != eCodeType.geteCodeList() && 0 != eCodeType.geteCodeList().size()) {
      // 当枚举信息不为空时进行新增操作
      for (ECode eCode : eCodeType.geteCodeList()) {
        eCode.seteCodeType(eCodeType.geteCodeType());
        if (null != eCode && StringUtils.isNotBlank(eCode.geteCode())) {
          if(eCode.geteCode().startsWith("{DELETE}_")){
            //如果是删除数据,则先删除翻译数据再删除原始数据
            eCode.seteCode(eCode.geteCode().substring(9));
            this.adminECodeTlDao.deleteECodeTlsByECodeTypeAndECode(eCode.geteCodeType(), eCode.geteCode());
            this.adminECodeDao.deleteECodeTlsByECodeTypeAndECode(eCode.geteCodeType(), eCode.geteCode());
          }else if(0< this.adminECodeDao.getECodeCount(eCode.geteCodeType(), eCode.geteCode())){
            //如果是存在记录则进行修改
            this.adminECodeDao.updateECode(eCode);
          }else{
            //如果不是修改也不是删除，则新增
            this.adminECodeDao.insertECode(eCode);
          }
        }
      }
    }

    return null;
  }

  @Override
  public BatchJobReport deleteECodeTypeAndAssociatedDataByECodeType(String eCodeType) {
    
    String cause = "";
    Boolean success = true;
    try {
      this.adminECodeTlDao.deleteECodeTlsByECodeType(eCodeType);
      this.adminECodeDao.deleteECodesByECodeType(eCodeType);
      this.adminECodeTypeDao.deleteECodeType(eCodeType);
    } catch (Exception e) {
      LOGGER.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      cause = e.getMessage();
      success = false;
    }
    return new BatchJobReport(eCodeType, cause, success, "zh");
  }

  @Override
  public Page selectECodeTypesPageByCondition(ECodeTypeCondition condition) {
    
    condition.setData(this.adminECodeTypeDao.selectECodeTypesByCondition(condition));
    condition.setTotalCount(this.adminECodeTypeDao.getECodeTypesCountByCondition(condition));
    return condition;
  }

  @Override
  public ECodeType getECodeTypeAndAssociatedDataByECodeType(String eCodeType) {
    
    ECodeType eCodeTypeObj = this.adminECodeTypeDao.getECodeTypeByPK(eCodeType);
    if (null != eCodeTypeObj) {
      eCodeTypeObj.seteCodeList(this.adminECodeDao.selectECodeListByECodeType(eCodeType));
    }
    return eCodeTypeObj;
  }

  @Override
  public List<ECode> selectEcodeListForTranslate(String eCodeType, String sourceLanguageCode, String toLanguageCode) {
    
    return this.adminECodeDao.selectEcodeListForTranslate(eCodeType, sourceLanguageCode, toLanguageCode);
  }

  @Override
  public void insertOrUpdateECodeTlBatch(List<ECodeTl> eCodeTlList) {
    
    for (ECodeTl eCodeTl : eCodeTlList) {
      this.adminECodeTlDao.deleteECodeTlsByECodeTypeAndECodeAndLanguageCode(eCodeTl.geteCodeType(), eCodeTl.geteCode(),
          eCodeTl.getLanguageCode());
      if (StringUtils.isNotBlank(eCodeTl.geteCodeText())) {
        this.adminECodeTlDao.insertECodeTl(eCodeTl);
      }
    }
  }

  @Override
  public ECodeType getECodeTypeByPK(String eCodeType) {
    
    return this.adminECodeTypeDao.getECodeTypeByPK(eCodeType);
  }
}
